/**
 * 
 */
package org.istt.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang.time.DateUtils;
import org.istt.domain.Field;
import org.istt.domain.FileManagement;
import org.istt.domain.User;
import org.istt.domain.WorkflowProcess;
import org.istt.service.FieldManager;
import org.istt.service.FileManager;
import org.istt.service.FileMgrManager;

import com.opensymphony.xwork2.ActionContext;

/**
 * @author Huy
 * 
 */
public class FileMgrAction extends BaseAction<FileMgrManager, FileManagement, Long> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8831350039530192869L;
	private String toDate;
	private String content;
	private String fieldId;
	private FieldManager fieldManager;
	private List<List<Object>> files;
	private String[] fileIds;
	private String[] fmgrIds;
	private FileManager fileManager;
	private List<List<Object>> filesToReturn;
	private FileManagement fileManagement;
	private Long id;
	
	public void setFileManager(FileManager fileManager) {
		this.fileManager = fileManager;
	}
	
	public List<List<Object>> getFilesToReturn() {
		return filesToReturn;
	}

	/**
	 * @return the fileIds
	 */
	public String[] getFileIds() {
		return fileIds;
	}

	/**
	 * @param fileIds the fileIds to set
	 */
	public void setFileIds(String[] fileIds) {
		List<String> lstFileIds = new ArrayList<String>();
		List<String> lstFmgrIds = new ArrayList<String>();
		for (String fileId : fileIds) {
			if (fileId.indexOf(",") != -1) {
				lstFmgrIds.add(fileId.split(",")[1]);
				lstFileIds.add(fileId.split(",")[0]);
			} else {
				lstFileIds.add(fileId);
			}
		}
		if (!lstFmgrIds.isEmpty()) fmgrIds = lstFmgrIds.toArray(new String[]{});
		this.fileIds = lstFileIds.toArray(new String[]{});
	}
	
	/**
	 * @return the fmgrIds
	 */
	public String[] getFmgrIds() {
		return fmgrIds;
	}

	/**
	 * @param fmgrIds the fmgrIds to set
	 */
	public void setFmgrIds(String[] fmgrIds) {
		this.fmgrIds = fmgrIds;
	}

	/**
	 * @return the files
	 */
	public List<List<Object>> getFiles() {
		return files;
	}

	/**
	 * @return the fields
	 */
	public List<Field> getFields() {
		return fieldManager.getList();
	}

	/**
	 * @param fieldManager
	 *            the fieldManager to set
	 */
	public void setFieldManager(FieldManager fieldManager) {
		this.fieldManager = fieldManager;
	}
	
	/**
	 * @return the toDate
	 */
	public String getToDate() {
		return toDate;
	}

	/**
	 * @param toDate
	 *            the toDate to set
	 */
	public void setToDate(String toDate) {
		this.toDate = toDate;
	}

	/**
	 * @return the content
	 */
	public String getContent() {
		return content;
	}

	/**
	 * @param content the content to set
	 */
	public void setContent(String content) {
		this.content = content;
	}
	
	/**
	 * @return the fieldId
	 */
	public String getFieldId() {
		return fieldId;
	}

	/**
	 * @param fieldId the fieldId to set
	 */
	public void setFieldId(String fieldId) {
		this.fieldId = fieldId;
	}

	@Override
	public Long getId() {
		return (fileManagement==null? id : fileManagement.getId());//id
	}
	
	public void setId(Long id) {
		this.id = id;
	}
	
	@Override
	protected void setSaveMessage() {
		ActionContext.getContext().getSession()
		.put("message", getText("file.saved"));		
	}

	@Override
	protected void setDeleteMessage() {
		
	}
	
	@Override
	public String search() throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("entering 'search' method");
		}
		Date t = StringUtils.isEmpty(toDate) ? null : DateUtils.parseDate(
				toDate, new String[] { getText("date.format") });
		files = ((FileMgrManager) mgr).search(NumberUtils.toLong(fieldId), content, t);
		return SUCCESS;
	}
	
	public String returnFile() throws Exception {
		filesToReturn = fileManager.searchByFileIds(fileIds);
		return SUCCESS;
	}
	
	public String getToEnd() throws Exception {
		User approver = (User)ActionContext.getContext().getSession().get(WebConstants.USER_INFO);
		((FileMgrManager) mgr).updateToEndStatus(fileIds, fmgrIds, approver);
		ActionContext.getContext().getSession().put("message", getText("returnFileList.success.message"));
		return SUCCESS;
	}

	
	public Date getDateYesterday() {
		Date d1 = new Date();
		Date d2 = new Date();
		d2.setTime(d1.getTime() - 1 * 24 * 60 * 60 * 1000);
		return d2;
	}	
	
	@Override
	public String save() {
		if (log.isDebugEnabled()) {
            log.debug("entering 'save' method");
        }
        
		FileManagement fm = ((FileMgrManager)mgr).get(id);
		fm.setApprovedDate(fileManagement.getApprovedDate());
		
		WorkflowProcess workflowProcess = fm.getWorkflowProcess();
		Integer curPosition = workflowProcess.getPosition();		
		List<FileManagement> fileMgnLst = ((FileMgrManager)mgr).searchFileMgrs(fm.getFile().getId(), true, curPosition+1);
		
		if (fileMgnLst.size()>0) {
			for (FileManagement fmn : fileMgnLst) {
				fmn.setReceivedDate(fileManagement.getApprovedDate());
			}
		}
        ((FileMgrManager)mgr).save(fm);
        setSaveMessage();
                
        return SUCCESS;
	}

	
	public String cancel() {
		if (log.isDebugEnabled()) {
            log.debug("entering 'cancel' method");
        }
        
        return SUCCESS;
	}	
	
	
	public void setFileManagement(FileManagement fileManagement) {
		this.fileManagement = fileManagement;
	}

	public FileManagement getFileManagement() {
		if (entity!= null && entity.getId() != null) {
			return entity;
		}		
		return fileManagement;
	}

	
	public String getFileId() {
		return ((FileMgrManager)mgr).get(id).getFile().getId();
	}

	public String getProcessStatusName() {
		return ((FileMgrManager)mgr).get(id).getWorkflowProcess().getProcessStatus().getDescription();
	}

	
}
